package org.yray.sip.rtp.sender;



import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import lombok.extern.slf4j.Slf4j;
import org.yray.sip.constant.Constants;
import org.yray.sip.log.AbstractLog;

import java.util.concurrent.TimeUnit;


@Slf4j
public class FastSenderCache {

    private FastSenderCache(){}

    private static Cache<Long,FastSender> fastSenderCache = Caffeine.newBuilder()
            .maximumSize(2048)
            .expireAfterWrite(Constants.CACHE_TIME, TimeUnit.SECONDS)
            .removalListener((RemovalListener<Long, FastSender>) (key, value, cause) ->
                    AbstractLog.log( key+"" , "FastSenderCache" , ", 删除原因:{}" ,cause.toString()))
            .build();

    public static  void put(Long sscr,FastSender fastSender)
    {

        fastSenderCache.put(sscr,fastSender);
        log.info("cache sipsession,callid={}，succ",sscr);
    }

    public static void removeCache(Long sscr)
    {
        fastSenderCache.invalidate(sscr);
    }
    public static FastSender get(Long ssrc)
    {
        //高并发起
        log.debug("get cache sipsession,callId={}",ssrc);
        FastSender fastSender= fastSenderCache.getIfPresent(ssrc);
        return fastSender;
    }
}
